package com.example.zdxk.netty;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;

/**
 * 用来进行设置出站解码器和入站编码器
 */
public class TcpInitializer extends ChannelInitializer<SocketChannel> {


    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();

        // 以("\n")为结尾分割的 解码器
        //pipeline.addLast("framer", new LengthFieldBasedFrameDecoder(65536，0，2));

        //pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
        //pipeline.addLast("frameEncoder", new LengthFieldPrepender(4));
        //创建分隔符缓冲对象ByteBuf，以0x0d,0x0a为分隔符
        ByteBuf delimiter1 = Unpooled.copiedBuffer(new byte[]{0x0d,0x0a});
        //ByteBuf delimiter2 = Unpooled.copiedBuffer(";!".getBytes());
        //ByteBuf delimiter3 = Unpooled.copiedBuffer(FormatUtil.hexStringToBytes("FAAF"));
        // 字符串解码和编码
        pipeline.addLast("decoder", new DelimiterBasedFrameDecoder(
                8192,false,delimiter1));
        //pipeline.addLast("encoder", new StringEncoder(Charset.forName("UTF8")));
       //心跳检测
       // pipeline.addLast("ping", new IdleStateHandler(25, 15, 10));
        // 自己的逻辑Handler
        pipeline.addLast("handler", new TcpInboundHandler());




    }}
